//
//  OMOrderSubmitProductTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/30.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMOrderSubmitProductTableCell.h"
#import "OMProduct.h"
#import "OMCartProduct.h"
@interface OMOrderSubmitProductTableCell ()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *prizeLabel;
@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation OMOrderSubmitProductTableCell

- (void)configureCellWithObj:(id<OMProductProtocol> )product
{

    [self.iconImageView kt_imageWithURLStr:product.image placeHolder:[UIImage imageNamed:@"placehold_item"] withSize:CGSizeZero];

    //标题
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@""];
    !product.brand.length?:[title appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"[%@]",product.brand]]];
    [title appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",product.name]]];
    !product.unit.length?:[title appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" (%@)",product.unit]]];
    self.titleLabel.attributedText = title;

    //价格
    NSMutableAttributedString *attributedPrize = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2f",[product.price floatValue]]];
    NSRange rangeOfDot = [attributedPrize.string rangeOfString:@"."];
    [attributedPrize addAttributes:@{NSFontAttributeName:SM_NFont(29)} range:NSMakeRange(1, rangeOfDot.location-1)];
    self.prizeLabel.attributedText = attributedPrize;

    //数量
    NSMutableAttributedString *attributedNum = [[NSMutableAttributedString alloc]initWithString:@"x " attributes:@{NSFontAttributeName:SM_NFont(26)}];
    [attributedNum appendAttributedString:[[NSAttributedString alloc] initWithString:[@(product.quantity)stringValue]]];
    self.numLabel.attributedText = attributedNum;
}


- (void)configure
{
    [super configure];
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.prizeLabel];
    [self.contentView addSubview:self.numLabel];

    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(20)));
        make.top.equalTo(@(SM_SIZE(20)));
        make.bottom.equalTo(@(SM_SIZE(-20)));
        make.width.equalTo(self.iconImageView.mas_height);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(SM_SIZE(20));
        make.top.equalTo(self.iconImageView.mas_top).offset(SM_SIZE(5));
        make.right.equalTo(@(SM_SIZE(-30)));
    }];

    [self.prizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconImageView.mas_bottom).offset(SM_SIZE(0));
        make.left.equalTo(self.titleLabel.mas_left);
    }];

    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-30)));
        make.centerY.equalTo(self.prizeLabel.mas_centerY);
    }];

}

#pragma mark - Getters
- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]init];

    }
    return _iconImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = SM_CFont(29);
        _titleLabel.textColor = SM_blackColor;
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

- (UILabel *)prizeLabel
{
    if (!_prizeLabel) {
        _prizeLabel = [UILabel new];
        _prizeLabel.font = SM_NFont(28);
        _prizeLabel.textColor = HEXCOLOR(@"#8F8F90");
    }
    return _prizeLabel;
}

- (UILabel *)numLabel
{
    if (!_numLabel) {
        _numLabel = [UILabel new];
        _numLabel.font = SM_NBFont(31);
        _numLabel.textColor = HEXCOLOR(@"#686868");
    }
    return _numLabel;
}


+ (CGFloat)cellHeight
{
    return SM_SIZE(150);
}

@end
